package com.sdk;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {
	
	public static String httpGet(String targetURL) {
		System.out.println("targetURL:" + targetURL);
    	try {
	        HttpURLConnection connection = null;
	        InputStream is = null;
	        try {
	            URL url = new URL(targetURL);
	            connection = (HttpURLConnection) url.openConnection();
	            int code = connection.getResponseCode();
	            if (code != 200) {
	                throw new Exception("Wrong response code!");
	            }
	    		
	            // 读取结果
	            is = connection.getInputStream();
	            String ret = new BufferedReader(new InputStreamReader(is, "UTF-8")).readLine();
	            System.out.println("recv=" + ret);
	            return ret;
	        } finally {
	            try {
	                if (is != null) {
	                    is.close();
	                }
	            } catch (Exception e) {
	            }
	            try {
	                if (connection != null) {
	                    connection.disconnect();
	                }
	            } catch (Exception e) {
	            }
	        }
    	}catch (Exception e) {
    		e.printStackTrace();
		}
    	return null;
	}
}
